package com.cms.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import com.cms.domain.Feedback;
import com.cms.entity.Div;
import com.cms.entity.DivField;
import com.cms.service.DivFieldService;
import com.cms.service.DivService;
import com.cms.utils.DbUtils;
import com.cms.utils.WebUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


/**
 * Controller - 表单数据
 * 
 * 
 * 
 */
@Controller("adminDivDataController")
@RequestMapping("/admin/div_data")
public class DivDataController extends BaseController {

	@Autowired
	private DivService divService;

	@Autowired
	private DivFieldService divFieldService;

	/**
	 * 列表
	 */
	@RequestMapping
	public String index(ModelMap modelMap){
		setListQuery();
		modelMap.addAttribute("divs", divService.findAll());
	    return getView("div_data/index");
	}

	/**
	 * 添加
	 */
	@RequestMapping("/add")
	public String add(Integer divId,ModelMap modelMap){
		Div div = divService.getById(divId);
		List<DivField> divFields = divFieldService.findList(divId);
		modelMap.addAttribute("divFields",divFields);
		modelMap.addAttribute("div", div);
		modelMap.addAttribute("divId", divId);
		return getView("div_data/add");
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	public Feedback save(Integer divId){
		Div div = divService.getById(divId);
		List<DivField> divFields = divFieldService.findList(divId);
		List<String> columnNames = new ArrayList<>();
		List<String> columnValues = new ArrayList<>();
		for(DivField divField : divFields){
			String value = WebUtils.getRequest().getParameter(divField.getName());
			columnNames.add(divField.getName());
			columnValues.add(value);
		}
		columnNames.add("createDate");
		columnNames.add("updateDate");
		columnValues.add(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
		columnValues.add(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
		SqlRunner.db().insert("insert into "+div.getTableName()+"("+StringUtils.join(columnNames,",")+") values('"+StringUtils.join(columnValues,"','")+"')");
		return Feedback.success(new HashMap<>());
	}
	
	/**
	 * 编辑
	 */
	@RequestMapping("/edit")
	public String edit(Integer id,Integer divId,ModelMap modelMap) {
		Div div = divService.getById(divId);
		List<DivField> divFields = divFieldService.findList(divId);
		Map<String,Object> map = SqlRunner.db().selectOne("select * from "+div.getTableName()+" where id="+id);
		modelMap.addAttribute("divFields",divFields);
		modelMap.addAttribute("div", div);
		modelMap.addAttribute("divData", map);
		modelMap.addAttribute("divId", divId);
		return getView("div_data/edit");
	}

	/**
	 * 更新
	 */
	@ResponseBody
	@RequestMapping("/update")
	public Feedback update(Integer id,Integer divId) {
		Div div = divService.getById(divId);
		List<DivField> divFields = divFieldService.findList(divId);
		String setSql=" set updateDate='"+DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss")+"'";
		for(DivField divField : divFields){
			String value = WebUtils.getRequest().getParameter(divField.getName());
			setSql+=","+divField.getName()+"='"+value+"'";
		}
		SqlRunner.db().update("update "+div.getTableName()+setSql);
		return Feedback.success(new HashMap<>());
	}
	
	/**
	 * 数据
	 */
	@RequestMapping("/data")
	public String data(Integer divId,Integer pageNumber,ModelMap modelMap) {
		setListQuery();
		if(pageNumber==null){
			pageNumber = 1;
		}
		Div div = divService.getById(divId);
		List<DivField> divFields = divFieldService.findList(divId);
		modelMap.addAttribute("divFields", divFields);
		String orderBySql = DbUtils.getOrderBySql("createDate desc");
		Page<Map<String,Object>> page = SqlRunner.db().selectPage(new Page<>(pageNumber, PAGE_SIZE), "select * from "+div.getTableName()+" where 1=1 "+orderBySql);
		modelMap.addAttribute("page", page);
		modelMap.addAttribute("divId", divId);
		return getView("div_data/data");
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Feedback delete(Integer divId,Integer[] ids) {
		Div div = divService.getById(divId);
		if(ArrayUtils.isNotEmpty(ids)){
			SqlRunner.db().delete("delete from "+div.getTableName()+" where id in("+StringUtils.join(ids, ",")+")");
		}
		return Feedback.success(new HashMap<>());
	}

}